package cn.rongdao.jrkabao.ui.IdentificationVerificate.IdentificationWithValidationItem;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frwidget.CustomStatusView;
import com.mifengkong.frwidget.PercentLinearLayout;
import com.moxie.client.model.MxParam;

import java.util.Map;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.identify.SocialSecurityVerifyDataBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.identify.SocialSecurityVerificateGetResultDataModel;

/**
 * Created by jiangyongxing on 2017/9/19.
 * 描述：
 */

public class IdentificationWithSocialSecurityActivity extends IdentificationWithValidationItemActivity {

    private static final String VERIFY_TYPE_SOCIAL_SECURITY = "11";

    @BindView(R.id.aivwvi_checking_pll)
    PercentLinearLayout mLoadingPll;
    @BindView(R.id.aivwvi_loading_imageview)
    CustomStatusView mCustomStatusView;
    @BindView(R.id.aivwvi_checking_tip)
    TextView mLoadingTip;

    private String mCateId;
    private String mProductId;

    private Map<String, String> loopNeedDataMap = new ArrayMap<>();

    public static void start(Context context, String productId) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.PRODUCT_ID, productId);
        FRStartActivity.start(context, IdentificationWithSocialSecurityActivity.class, bundle);
    }


    @Override
    protected void initView() {
        super.initView();
        initMXData();
        mCateId = InfoConstant.SOCIAL_SECURITY_CATE_ID;
        mProductId = getIntent().getStringExtra(InfoConstant.PRODUCT_ID);
        initLoopNeedData();
        mValidationItemUIComponent.setTitle("第二步  社保验证");
        titleBarUIComponent.initTitleBlack("社保验证");
        mValidationItemUIComponent.setContent(R.drawable.icon_social_security, "登录社保账户", "请登录本人的社保账户");
        mLoadingTip.setText("正在验证社保\n请稍候...");
        mValidationItemUIComponent.setSubmitOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startMXActiity();
            }
        });
    }


    void startMXActiity() {
        loopNeedDataMap.put("verify_type", VERIFY_TYPE_SOCIAL_SECURITY);
        startMoxieActivity();
    }

    private void initLoopNeedData() {
        if (!TextUtils.isEmpty(mCateId)) {
            loopNeedDataMap.put("cate_id", mCateId);
        }
        if (!TextUtils.isEmpty(mProductId)) {
            loopNeedDataMap.put("product_id", mProductId);
        }

    }

    private void initMXData() {
        moXieManager.initOnlineBankingMoXieContent(MxParam.PARAM_TASK_SECURITY);
    }

    @Override
    protected void successReturn() {
        super.successReturn();
        mLoadingPll.setVisibility(View.VISIBLE);
        mCustomStatusView.loadLoading();
    }

    @Override
    protected void baseLooperMethod() {
        super.baseLooperMethod();
        SocialSecurityVerificateGetResultDataModel.getInstance().execute(loopNeedDataMap, new FRBaseObserver<SocialSecurityVerifyDataBean>(this) {
            @Override
            protected void onBaseNext(final SocialSecurityVerifyDataBean data) {
                if (data.getVerify_status() == 1) {
                    stopRunning();
                    finish();
                    setCalculateLimitViewGone();
                }
            }
        });
    }

    private void setCalculateLimitViewGone() {
        mLoadingPll.setVisibility(View.INVISIBLE);
        mCustomStatusView.clearAnimator();
    }
}
